# _*_ coding : utf-8 _*_
# @Time: 2024/7/16 9:45
# @Author : 石文帅	
# @File : BasePage
# @Project : python-interface-automation
import ast
import configparser

import jsonpath
import pandas
import os
import requests


def send_method(url, method, params=None, data=None, json=None, headers=None):
    if method == "get":
        response = requests.get(url=url, params=params, headers=headers)

    elif method == "post":
        if data is None and json is None:
            response = requests.post(url=url, headers=headers)
        elif data is None and json is not None:
            response = requests.post(url=url, json=json, headers=headers)
        elif data is not None and json is None:
            response = requests.post(url=url, data=data, headers=headers)
        else:
            response = None
            print("输入的传参方式错误目前只支持，data 和 json 两种方式")
    else:
        response = None
        print("输入的请求方式错误目前只支持，get 和 post 两种方式")

    # 获取状态码，响应头，响应体，响应时间
    if response is not None:
        result = {}
        result["code"] = response.status_code  # 获取状态码
        result["headers"] = response.headers
        try:
            result["body"] = response.json()
        except:
            result["body"] = response.text
        result["response_time"] = response.elapsed.microseconds / 1000
        return result
    else:
        return response


def get_keyword(source_data, target_data):
    """
    获取关键字
    :param source_data: 源json字符串
    :param target_data: 提取的目标数据
    :return:
    """
    try:
        return jsonpath.jsonpath(source_data, f'$..{target_data}')[0]
    except:
        return None


def get_keywords(source_data, target_data):
    '''
    获取多个关键字
    :param source_data:源json字符串
    :param target_data: 提取的目标数据
    :return:
    '''

    try:
        return jsonpath.jsonpath(source_data, f'$..{target_data}')
    except:
        return None


def get_db_data(onelevel, twolevel):
    '''
    读取ini配置文件
    :return:
    '''
    cfg = configparser.ConfigParser()

    cfg.read('../db.ini', encoding='utf-8')

    # cfg.read(r'D:\Users\Administrator\PycharmProjects\interface-pytest\Config\db.ini',encoding='utf-8')
    #
    # data = cfg.items('databases')
    # data = dict(data)
    # data['port'] = int(data['port'])
    return cfg[onelevel][twolevel]


def set_db_data(onelevel, twolevel, value):
    '''
        修改ini配置文件
        :return:
    '''
    cfg = configparser.ConfigParser()
    cfg.read('../db.ini', encoding='utf-8')
    # 修改配置项的值
    cfg.set(onelevel, twolevel, value)
    with open('../db.ini', 'w') as configfile:
        cfg.write(configfile)




def str_to_dict1(string):
    '''
    python字符串转为字典
    :param string:
    :return:
    '''
    # 去除字符串中的空格和花括号
    string = string.replace(" ", "").strip("{}")

    # 利用split函数将字符串分割成键值对，并存储到一个列表中
    pairs = string.split(",")

    # 将键值对分别存储到字典中
    result = {}
    for pair in pairs:
        key, value = pair.split(":")
        result[key] = value

    return result

def str_to_dict(string):
    '''
    python字符串转为字典
    :param string:
    :return:
    '''
    #去掉字符串中所有的换行符号
    #string = str.replace(string)
    # 利用ast模块的literal_eval函数将字符串转化为字典
    result = ast.literal_eval(string)
    return result
if __name__ == '__main__':
    pass
